// pages/detail/detail.js
const $api = require("../../api/index.js");

Page({
    data: {
        id: "",
        film: {},
        filmCredits: [],
        filmComments: [],
        // 评论分页
        start: 0,
        count: 10,
        total: 0,
        loading: false,
        end: false
    },
    onLoad(options) {
        this.data.id = options.id;
        this.loadFilmDetailData();
        this.loadFilmCreditsData();
        this.loadFilmCommentData();
    },
    async loadFilmDetailData() {
        try {
            let res = await $api.loadFilmDetail(this.data.id);
            // console.log(res);
            this.setData({
                film: res.data
            });
        } catch (error) {
            console.log(error);
        }
    },
    async loadFilmCreditsData() {
        try {
            let res = await $api.loadFilmCredits(this.data.id);
            let items = res.data.items;
            // items = items.filter( it => it.category == "演员" );
            // console.log(res);
            this.setData({
                filmCredits: items
            });
        } catch (error) {
            console.log(error);
        }
    },
    async loadFilmCommentData() {
        try {
            this.setData({
                loading: true
            });
            let res = await $api.loadFilmComment(this.data.id, this.data.start, this.data.count);
            // console.log(res.data);
            let data = res.data;
            this.data.filmComments.push( ...data.interests );
            this.setData({
                filmComments: this.data.filmComments,
                start: data.start + this.data.count, // 下一次的开始
                total: data.total
            });
        } catch (error) {
            console.log(error);
        }
        let end = false;
        if(this.data.start >= this.data.total) {
            end = true;
        }
        this.setData({
            loading: false,
            end: end
        });
    },
    addLove() {
        // 加载以前的收藏数据
        let loveList = wx.getStorageSync('loveList');
        // 判断以前有没有收藏,如果没有收藏初始化收藏列表
        if (!Array.isArray(loveList)) {
            loveList = [];
        }
        for (let item of loveList) {
            if (item.id == this.data.film.id) {
                wx.showToast({
                    title: '已经收藏',
                    icon: 'none'
                });
                return;
            }
        }
        loveList.push(this.data.film);
        wx.setStorageSync('loveList', loveList);
        wx.showToast({
            title: '收藏成功',
            icon: 'none'
        });
    },
    async onReachBottom() {
        //页面上拉触底事件的处理函数
        if(this.data.start < this.data.total) {
            //console.log('onReachBottom');
            await this.loadFilmCommentData();
            //console.log('loaded data');
        }
    },
    showCoverImg() {
        getApp().previewImages(this.data.film.cover.image.large.url);
    },
    showCreditsImg(event) {
        let index = event.currentTarget.dataset.i;
        let arr = this.data.filmCredits;
        let imageArr = arr.map( c => c.avatar.large );
        getApp().previewImages(imageArr, index);
    },
    async onPullDownRefresh() {
        this.data.filmComments = [];
        this.data.start = 0;
        this.data.total = 0;
        await this.loadFilmDetailData();
        await this.loadFilmCreditsData();
        await this.loadFilmCommentData();
        wx.stopPullDownRefresh();
    }
})